<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;

class DrugDictionary extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $table = 'st_drug_dictionary';

    public function doctor()
    {
        return $this->belongsTo(User::class, 'doctor_id');
    }

    public function isInsulin()
    {
        return $this->is_insulin;
    }

    public static function group()
    {
        $groups = app('field')->get('drug.group');

        return static::all()->groupBy('group')->map(function (Collection $drugs, $groupId) use ($groups) {

            return [
                'data'  => $drugs,
                'title' => $groupId ? $groups[$drugs->first()->type][$groupId] : $groups[$groupId]
            ];
        });
    }
}
